package com.neusoft.neuiotms.dcam.fixed.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.ContractItemDTO;
import com.neusoft.neuiotms.dcam.fixed.model.AssetType;
import com.neusoft.neuiotms.dcam.fixed.model.Contract;
import com.neusoft.neuiotms.dcam.fixed.model.ContractItem;
import com.neusoft.neuiotms.dcam.fixed.model.Product;
import com.neusoft.neuiotms.dcam.fixed.model.SpareType;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetTypeRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.ContractItemRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.ContractRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.ProductRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.SpareTypeRepository;

import io.micrometer.core.instrument.util.StringUtils;

@Component
public class ContractItemConverter extends AbstractConverter<ContractItem, ContractItemDTO> {

    @Autowired
    private ContractItemRepository contractItemRepository;
    @Autowired
    private AssetTypeRepository assetTypeRepository;
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private ContractRepository contractRepository;
    @Autowired
    private SpareTypeRepository spareTypeRepository;

    @Override
    public ContractItem toModel(final ContractItemDTO dto) {

        ContractItem model;

        if (dto.isNew()) {
            model = new ContractItem();
        } else {
            model = this.contractItemRepository.getOne(dto.getId());
        }

        model.setAmount(dto.getAmount() == null ? 0.0D : dto.getAmount());
        model.setCount(dto.getCount());
        model.setDescription(dto.getDescription());
        model.setAcceptedCount(dto.getAcceptedCount());
        model.setType(dto.getType());
        model.setStandard(dto.getStandard());

        if ("1".equals(dto.getType())) {
            // 资产分类
            if (StringUtils.isNotEmpty(dto.getAssetTypeCode())) {
                final AssetType assetType = this.assetTypeRepository.findByCode(dto.getAssetTypeCode());
                model.setAssetType(assetType);
            }

            // 产品
            if (null != dto.getProductId()) {
                final Product product = this.productRepository.getOne(dto.getProductId());
                model.setProduct(product);
            }
        } else if ("2".equals(dto.getType())) {
            if (StringUtils.isNotEmpty(dto.getAssetTypeCode())) {
                final SpareType type = this.spareTypeRepository.findByCode(dto.getAssetTypeCode());
                model.setSpareType(type);
            }

            //            if (null != dto.getProductId()) {
            //                final GoodsFile goodsfile = this.goodsFileRepository.getOne(dto.getProductId());
            //                model.setGoodsFile(goodsfile);
            //            }
        }

        //合同
        if (null != dto.getContractId()) {
            final Contract contract = this.contractRepository.getOne(dto.getContractId());
            model.setContract(contract);
        }

        return model;
    }

    @Override
    public ContractItemDTO toDTO(final ContractItem model, final boolean forListView) {

        final ContractItemDTO dto = new ContractItemDTO();

        dto.setId(model.getId());
        dto.setAmount(model.getAmount());
        dto.setCount(model.getCount());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setDescription(model.getDescription());
        dto.setAcceptedCount(model.getAcceptedCount());
        dto.setType(model.getType());
        dto.setStandard(model.getStandard());

        if ("1".equals(model.getType())) {
            // 分类
            if (model.getAssetType() != null) {
                dto.setAssetTypeCode(model.getAssetType().getCode());
                dto.setAssetTypeName(model.getAssetType().getName());
            }

            // 产品
            if (model.getProduct() != null) {
                dto.setProductId(model.getProduct().getId());
                dto.setProductName(model.getProduct().getName());
                //                dto.setProductDTO(this.productConverter.toDTO(model.getProduct()));
                dto.setBrand(model.getProduct().getBrand());
                dto.setMeasureUnit(model.getProduct().getMeasureUnit());
            }
        } else if ("2".equals(model.getType())) {
            if (null != model.getSpareType()) {
                dto.setAssetTypeCode(model.getSpareType().getCode());
                dto.setAssetTypeName(model.getSpareType().getName());
            }

            //            if (model.getGoodsFile() != null) {
            //                dto.setProductId(model.getGoodsFile().getId());
            //                dto.setProductName(model.getGoodsFile().getName());
            //                //                dto.setStandard(model.getGoodsFile().getSpecification());
            //            }

        }

        // 合同
        if (model.getContract() != null) {
            dto.setContractId(model.getContract().getId());
            dto.setContractName(model.getContract().getName());
        }

        return dto;
    }

}
